package org.tgit.gateway.config;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
public class DruidConfig {
	private static final Logger LOGGER = LoggerFactory.getLogger(DruidConfig.class);
    @Value("${spring.datasource.dbType}")
	private String dbType;
    @Value("${spring.datasource.url}")
	private String url;
    @Value("${spring.datasource.username}")
	private String username;
    @Value("${spring.datasource.password}")
	private String password;
    @Value("${spring.datasource.driverClassName}")
	private String driverClassName;
    @Value("${spring.datasource.initialSize}")
	private int initialSize;
    @Value("${spring.datasource.maxActive}")
	private int maxActive;
    @Value("${spring.datasource.minIdle}")
	private int minIdle;
    @Value("${spring.datasource.maxWait}")
	private int maxWait;
    @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
	private int timeBetweenEvictionRunsMillis;
    @Value("${spring.datasource.minEvictableIdleTimeMillis}")
	private int minEvictableIdleTimeMillis;
    @Value("${spring.datasource.validationQuery}")
	private String validationQuery;
    @Value("${spring.datasource.testOnBorrow}")
	private boolean testOnBorrow;
    @Value("${spring.datasource.testWhileIdle}")
	private boolean testWhileIdle;
    @Value("${spring.datasource.testOnReturn}")
	private boolean testOnReturn;
    @Value("${spring.datasource.poolPreparedStatements}")
	private boolean poolPreparedStatements;
    @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
	private int maxPoolPreparedStatementPerConnectionSize;
    @Value("${spring.datasource.filters}")
	private String filters;
	
	

	/**
	 * 数据源连接池
	 * 
	 * @return
	 */
	@Bean(destroyMethod="close",initMethod="init")
	public DataSource mysqlDtaSource() {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDbType(dbType);
		dataSource.setUrl("jdbc:mysql://"+url+"?autoReconnect=true&useCompression=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true");
		dataSource.setUsername(username);
		dataSource.setPassword(password);
		dataSource.setDriverClassName(driverClassName);
		dataSource.setInitialSize(initialSize);
		dataSource.setMaxActive(maxActive);
		dataSource.setMinIdle(minIdle);
		dataSource.setMaxWait(maxActive);
		dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
		dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
		dataSource.setValidationQuery(validationQuery);
		dataSource.setTestOnBorrow(testOnBorrow);
		dataSource.setTestWhileIdle(testWhileIdle);
		dataSource.setTestOnReturn(testOnReturn);
		dataSource.setPoolPreparedStatements(poolPreparedStatements);
		dataSource.setMaxPoolPreparedStatementPerConnectionSize(20);
		try {
			dataSource.setFilters(filters);
		} catch (SQLException e) {
			LOGGER.error("filtes", e);
			e.printStackTrace();
		}
		return dataSource;
	}
}
